//
//  StatusOriginTableView.swift
//  sina
//
//  Created by 江先铨 on 15/11/16.
//  Copyright © 2015年 jxqstatic. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

let StatuCellMargin: CGFloat = 12
let StatuCellIconWidth: CGFloat = 35

class StatusOriginTableView: UIView {
    
    //原创微博的底部约束, 用来记录底部约束  后续根据是否有配图, 来动态修改
    var bottomConstraints: Constraint?
    
    var status: StatusModel? {
        didSet {  //属性监听器 当属性的值发生改变的适合调用下面的代码 执行下面的语句
        nameLabel.text = status!.user?.name
        iconImage.sd_setImageWithURL(status?.user?.headImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
        memberIconView.image = status?.user?.mbrankImage
        verifiedIconView.image = status?.user?.verified_type_Image
        
        //时间的处理对象
        timeLabel.text = NSDate.sinaDate(status?.created_at ?? "")?.fullDescription()
        sourceLabel.text = status?.source
        contenLabel.text = status?.text
            //需要把原来的约束给移除掉 不然的会产生复用的问题 会影响cell 的复用 原来的图片会被覆盖掉 所以需要把数据给移除掉
            self.bottomConstraints?.uninstall()
            //首先在这里判断当前的picurls这个值是否为空  不为空 并且.count是有值的话
            //说名有配图视图   设置有配图视图的约束
            if let picurls = status?.imageURLs  where picurls.count > 0 {
                //给配图视图设置数据源 设置数据
                pictrue.imageURLs = status?.imageURLs
                //显示配图视图
                pictrue.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //有配图视图的约束 需要设置配图视图的约束
                      self.bottomConstraints =  make.bottom.equalTo(pictrue.snp_bottom).offset(StatuCellMargin).constraint
                })
            } else  {
            
             //没有配图视图
             //更新配图视图约束
                //隐藏配图视图
               pictrue.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //如果没有配图视图 底部约束就需要改成 相对于内同的底部约束
                    self.bottomConstraints =  make.bottom.equalTo(contenLabel.snp_bottom).offset(StatuCellMargin).constraint
                })
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    
    private func setupUI() {
    
       backgroundColor = UIColor(white: 0.95, alpha: 1.0)
        sepView.backgroundColor = UIColor(white: 0.8, alpha: 1)
        addSubview(sepView)
        addSubview(iconImage)
        addSubview(nameLabel)
        addSubview(memberIconView)
        addSubview(verifiedIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contenLabel)
        //添加配图视图
        addSubview(pictrue)
        
        
        //设置视图view 的约束 
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            //设置高度为间隔的高度
            make.height.equalTo(StatusCellMargin)
        }
       
        //设置头像的约束 大小为 35
        iconImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(snp_top).offset(StatuCellMargin * 2)
            make.left.equalTo(snp_left).offset(StatuCellMargin)
            make.height.equalTo(StatuCellIconWidth)
            make.width.equalTo(StatuCellIconWidth)
        }
        //设置名字的约束
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_top)
            make.left.equalTo(iconImage.snp_right).offset(StatuCellMargin)
        }
        //设置会员的约束
       memberIconView.snp_makeConstraints { (make) -> Void in
         make.top.equalTo(nameLabel.snp_top)
         make.left.equalTo(nameLabel.snp_right).offset(StatuCellMargin)
        }
        //认证的图像
        verifiedIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImage.snp_right)
            make.centerY.equalTo(iconImage.snp_bottom)
        }
        
        //时间的约束
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconImage.snp_bottom)
            make.left.equalTo(iconImage.snp_right).offset(StatuCellMargin)
        }
        //来源的约束
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(StatuCellMargin)
        }
        //内容的约束
        contenLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_bottom).offset(StatuCellMargin)
            make.left.equalTo(iconImage)
        }
        
        
        pictrue.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contenLabel.snp_bottom).offset(StatuCellMargin)
            make.left.equalTo(contenLabel.snp_left)
            //这个约束是需要动态的设置的不需要我们手动来设置固定的约束  在上一个文件夹中已经判断了约束 所以说这里的约束可以伤处
//            make.width.equalTo(100)
//            make.height.equalTo(100)
        }
        //原创微博的底部相对于微博正文的底部对齐
        self.snp_makeConstraints { (make) -> Void in
            //记录底部视图的约束
          self.bottomConstraints =  make.bottom.equalTo(pictrue.snp_bottom).offset(StatuCellMargin).constraint
        }
        
       // backgroundColor = UIColor.redColor()
    
    }
    
    //懒加载分割的view 
    private lazy var sepView:  UIView = UIView()
    
    private lazy var iconImage: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var nameLabel: UILabel = UILabel(title: "青葱岁月", size: 17, color: UIColor.darkGrayColor())
    private lazy var verifiedIconView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    private lazy var memberIconView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    private lazy var timeLabel: UILabel = UILabel(title: "10:10", size: 10, color: UIColor.orangeColor())
    private lazy var sourceLabel: UILabel = UILabel(title: "道光皇帝", size: 14, color: UIColor.lightGrayColor())
    private lazy var contenLabel: UILabel = UILabel(title:  "内心的工程师内心的工程师内心的工程师内心的工程师内心的工程师内心的工程师内心的工程师内心的工程师内心的工程师内心的工程师内心的工程师内心的工程师内心的工程师", size: 14, color: UIColor.lightGrayColor(), margin: 10)

    //初始化一个配图视图出来
    private lazy var pictrue:  StatusPictureView = StatusPictureView()
}
